<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="店铺类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            :defaultActiveFirstOption="false"
            :showArrow="false"
            placeholder="指定店铺类型"
            @change="handlerTypeChange"
            notFoundContent="null"
            v-decorator="['shopTypeId', { rules: [{ required: true, message: '请指定店铺类型' }] }]"
          >
            <a-select-opt-group v-for="item in shopTypeList" :key="item.id">
              <span slot="label">
                <a-icon type="user"/>
                {{ item.name }}
              </span>
              <a-select-option :key="n.id" v-for="n in item.children">{{ n.name }}</a-select-option>
            </a-select-opt-group>
          </a-select>
        </a-form-item>
        <a-form-item label="店铺类型图" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-upload
            action="/mall-serve/v1/sys/common/uploadImage"
            name="file"
            listType="picture-card"
            :fileList="picList"
            @preview="handlePicPreview"
            @change="handlePicChange"
            :showUploadList="true"
          >
            <div v-if="picList.length < 1">
              <a-icon type="plus"/>
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
          <a-modal :visible="previewVisible" :footer="null" @cancel="handlePicCancel">
            <img alt="example" :src="previewImage">
          </a-modal>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序值">
          <a-input-number v-decorator="['sort', validatorRules.sort]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-radio-group v-decorator="['status', validatorRules.status]">
            <a-radio-button value="0">开启</a-radio-button>
            <a-radio-button value="1">关闭</a-radio-button>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import moment from 'moment'
import { httpAction, getAction } from '@/api/manage'
export default {
  name: 'ShopTypeNameModal',
  data() {
    return {
      imageDownloadUri: '/mall-serve/v1/sys/common/getImageByUri?imageUri=',
      typeName: '',
      shopTypeList: [],
      previewVisible: false,
      previewImage: '',
      picList: [],
      isEdit: false,
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        shopType: { rules: [{ required: true, message: '请输入商铺类型  !' }] },
        status: { rules: [{ required: true, message: '请选择状态  !' }], initialValue: '0' },
        sort: { rules: [{ required: true, message: '请选择状态  !' }], initialValue: 0 }
      },
      url: {
        add: '/shoptypename/web/add',
        edit: '/shoptypename/web/edit'
      }
    }
  },
  created() {},
  methods: {
    handlerTypeChange(value) {
      console.log(value)
      let list = this.shopTypeList
      for (let i in list) {
        let temp = list[i]
        if (temp.children != null) {
          for (let j in temp.children) {
            if (temp.children[j].id == value) {
              this.typeName = temp.children[j].name
            }
          }
        }
      }
    },
    initTypeList(value) {
      getAction('/shopType/getShopTypeList').then(res => {
        if (res.success) {
          this.shopTypeList = res.result
        }
        if (res.code === 500) {
          this.$message.warning(res.message)
        }
      })
    },
    handlePicPreview(file) {
      console.log(file)
      if (this.isEdit) {
        this.previewImage = file.thumbUrl
        this.previewVisible = true
      } else {
        this.previewImage = file.url || file.thumbUrl
        this.previewVisible = true
      }
    },
    handlePicCancel() {
      this.previewVisible = false
    },
    handlePicChange({ fileList }) {
      console.log(fileList)
      this.picList = fileList
    },
    initUploadParams() {
      this.previewImage = ''
      this.picList = []
    },
    add() {
       this.isEdit = false
      this.visible = true
      this.initUploadParams()
      this.form.resetFields()
      this.initTypeList()
      this.typeName = ''
    },
    edit(record) {
      this.visible = true
      this.initTypeList()
      this.initUploadParams()
      this. handlerTypeChange(record.shopTypeId)
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'sort',
            'picUrl',
            'shopTypeId',
            'shopTypeName',
            'status'))
        //时间格式化
        if (this.model.picUrl != null && this.model.picUrl != undefined) {
          this.picList.push({
            uid: Math.random(),
            name: this.model.picUrl,
            url: this.imageDownloadUri + this.model.picUrl,
            status: 'done',
            type: 'image/*'
          })
        }
        this.form.setFieldsValue({ sellerId: this.model.id })
         this.isEdit = true
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          console.log("111111"+that.picList)
          if (that.picList == undefined || that.picList.length == 0) {
            this.modalVisible = true
            that.modalText = '请上传一张分类图标'
            that.confirmLoading = false
            return
          }
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          formData.shopTypeName = that.typeName
          //时间格式化
          if(that.picList[0].response !=undefined && that.picList[0].response !=null){
           formData.picUrl = that.picList[0].response.result.imageUri
          }
          console.log(formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style lang="less" scoped></style>
